package com.gjintel.business.proj.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.gjintel.business.proj.dao.IDepositReturnDao;
import com.gjintel.business.proj.model.DepositReturn;
import com.gjintel.sdk.base.dao.HibernateGenericDAO;
import com.gjintel.sdk.base.dao.Page;

@Repository
public class DepositReturnDaoImpl extends HibernateGenericDAO implements IDepositReturnDao {

	@Override
	public DepositReturn getDepositReturnByProjIdAndCompanyName(String projId, String companyName) {
		StringBuffer hql = new StringBuffer("from DepositReturn dr where dr.pk.projectId=? and dr.pk.depositType=0");
		List<Object> params = new ArrayList<Object>();
		params.add(projId);
		if (companyName != null) {
			hql.append(" and dr.pk.companyName=?");
			params.add(companyName);
		}
		List list = this.find(hql.toString(), params.toArray());
		return list != null && list.size() > 0 ? (DepositReturn) list.get(0) : null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepositReturn> getDepositReturnList(String projectId) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from DepositReturn dr where dr.pk.projectId=? and dr.status=0 and dr.pk.depositType=0";
		params.add(projectId);
		return (List<DepositReturn>) this.find(hql.toString(), params.toArray());
	}

	@Override
	public DepositReturn getDepositReturn(String loginName, String projectId) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from DepositReturn dr where dr.pk.companyName=? and dr.pk.projectId=? and dr.pk.depositType=0";
		params.add(loginName);
		params.add(projectId);
		return (DepositReturn) this.findUnique(hql.toString(), params.toArray());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepositReturn> getUnOptList(String projectId,int depositType) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from DepositReturn dr where dr.pk.projectId=? and dr.status<>1 and dr.status<>2 and dr.pk.depositType=?";
		params.add(projectId);
		params.add(depositType);
		return (List<DepositReturn>) this.find(hql.toString(), params.toArray());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepositReturn> getReturnList(String projectId, int depositType) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from DepositReturn dr where dr.pk.projectId=? and dr.pk.depositType=?";
		params.add(projectId);
		params.add(depositType);
		return (List<DepositReturn>) this.find(hql.toString(), params.toArray());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepositReturn> getExcuteDeposit(String projectId, String winningId, Integer depositType) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from DepositReturn dr where dr.pk.projectId =? and dr.bidderId=? and dr.pk.depositType=?";
		params.add(projectId);
		params.add(winningId);
		params.add(depositType);
		return this.find(hql, params.toArray());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DepositReturn> getExcuteDepositList(String projectId, Integer depositType,
			Page page) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from DepositReturn dr where dr.pk.projectId =?and dr.pk.depositType=?";
		params.add(projectId);
		params.add(depositType);
		return this.findAsPage(hql.toString(), page, params.toArray());
	}


	@SuppressWarnings("unchecked")
	@Override
	public DepositReturn getExcuteDeposit(String projectId, Integer depositType) {
		List<Object> params = new ArrayList<Object>();
		String hql = "from DepositReturn dr where dr.pk.projectId =?and dr.pk.depositType=?";
		params.add(projectId);
		params.add(depositType);
		return (DepositReturn)this.findUnique(hql.toString(),params.toArray());
	}

	@Override
	public DepositReturn getDepositListByHstSeqNum(String hstSeqNum) {
		List<Object> params = new ArrayList<Object>();
        String hql = "from DepositReturn dr where dr.pk.hstSeqNum= ?";
        params.add(hstSeqNum);
		return (DepositReturn)this.findUnique(hql.toString(),params.toArray());
	}
}
